package handler

import (
	"context"
	"encoding/json"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/global"
	"mxshop_srvs/goods_srv/model"
	"mxshop_srvs/goods_srv/proto"
)

// 商品分类接口
// GetAllCategorysList 获取所有分类
func (g *GoodsServer) GetAllCategorysList(ctx context.Context, in *emptypb.Empty) (*proto.CategoryListResponse, error) {
	// 实现获取所有分类的逻辑
	var categorys []model.Category
	global.DB.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&categorys)
	b, _ := json.Marshal(categorys)
	return &proto.CategoryListResponse{JsonData: string(b)}, nil
}

// GetSubCategory 获取子分类
func (g *GoodsServer) GetSubCategory(ctx context.Context, in *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {
	// 实现获取子分类的逻辑
	var category model.Category
	if result := global.DB.First(&category, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "分类不存在")
	}
	categoryListResponse := proto.SubCategoryListResponse{}
	categoryListResponse.Info = &proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryID,
		Level:          category.Level,
		IsTab:          category.IsTab,
	}
	var subCategorys []model.Category
	preloads := "SubCategory"
	/*if category.Level == 1 {
		preloads = "SubCategory.SubCategory"
	}*/
	global.DB.Where(&model.Category{ParentCategoryID: category.ID}).Preload(preloads).Find(&subCategorys)
	subCategorysResponse := []*proto.CategoryInfoResponse{}
	for _, subCategory := range subCategorys {
		subCategorysResponse = append(subCategorysResponse, &proto.CategoryInfoResponse{
			Id:             subCategory.ID,
			Name:           subCategory.Name,
			ParentCategory: subCategory.ParentCategoryID,
			Level:          subCategory.Level,
			IsTab:          subCategory.IsTab,
		})
	}
	categoryListResponse.SubCategorys = subCategorysResponse
	return &categoryListResponse, nil
}

// CreateCategory 创建分类
func (g *GoodsServer) CreateCategory(ctx context.Context, in *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {
	// 实现创建分类的逻辑
	category := model.Category{}
	category.Name = in.Name
	category.Level = in.Level
	if in.Level != 1 {
		category.ParentCategoryID = in.ParentCategory
	}
	category.IsTab = in.IsTab
	global.DB.Create(&category)
	return &proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryID,
		Level:          category.Level,
		IsTab:          category.IsTab,
	}, nil
}

// DeleteCategory 删除分类
func (g *GoodsServer) DeleteCategory(ctx context.Context, in *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	// 实现删除分类的逻辑
	if result := global.DB.Delete(&model.Category{}, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "分类不存在")
	}
	return &emptypb.Empty{}, nil
}

// UpdateCategory 更新分类
func (g *GoodsServer) UpdateCategory(ctx context.Context, in *proto.CategoryInfoRequest) (*emptypb.Empty, error) {
	// 实现更新分类的逻辑
	var category model.Category
	// 判断分类是否存在
	if result := global.DB.First(&category, in.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "分类不存在")
	}
	// 更新分类信息
	if in.Name != "" {
		category.Name = in.Name
	}
	if in.Level != 0 {
		category.Level = in.Level
	}
	if in.ParentCategory != 0 {
		category.ParentCategoryID = in.ParentCategory
	}
	if in.IsTab != false {
		category.IsTab = in.IsTab
	}
	global.DB.Save(&category)
	return &emptypb.Empty{}, nil
}
